package xj.graph2d.template.uml;

import xj.graph2d.AbstractModelListener;
import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.GraphNodeBase;
import xj.graph2d.shapes.uml.UMLUtil;
import xj.model.uml.UMLPackage;

public class UMLPackageModelListener extends AbstractModelListener {

  public UMLPackageModelListener() {
  }

  public UMLPackageModelListener(Object model, DrawShape shape) {
    super(model, shape);
  }

  @Override
  public void updateModel() {
    //System.out.println("UMLPackageModelListener.updateModel()");

    if (shape instanceof GraphNodeBase && model instanceof UMLPackage) {
      GraphNodeBase pkgnode = (GraphNodeBase) shape;
      UMLPackage pkgmodel = (UMLPackage) model;
      // String name = pkgnode.getLabel();
      int headerRole = (pkgnode.isAcceptSubshape() ? DrawShapeConstants.ROLE_NODE_TITLE_LABEL
	  : DrawShapeConstants.ROLE_NODE_LABEL);
      String name = UMLUtil.getName(pkgnode, headerRole);
      String st = UMLUtil.getStereotypes(pkgnode, headerRole);
      if (name != null) {
	pkgmodel.setName(name);
      }
      pkgmodel.setStereotypes(st);
    }
  }

  @Override
  public void updateShape() {
    //System.out.println("UMLPackageModelListener.updateShape()");

    if (shape instanceof GraphNodeBase && model instanceof UMLPackage) {
      GraphNodeBase pkgnode = (GraphNodeBase) shape;
      UMLPackage pkgmodel = (UMLPackage) model;
      String name = pkgmodel.getName();
      String st = pkgmodel.getStereotypeString();

      int headerRole = (pkgnode.isAcceptSubshape() ? DrawShapeConstants.ROLE_NODE_TITLE_LABEL
	  : DrawShapeConstants.ROLE_NODE_LABEL);
      if (name != null) {
	UMLUtil.setName(pkgnode, headerRole, name);
	// pkgnode.setLabel(name);
      }
      UMLUtil.setStereotypes(pkgnode, headerRole, st);
    }
  }

}
